package com.lightMS.agenda;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.lightMS.agenda.be.Event;
import com.lightMS.agenda.bl.EventBL;
import com.lightMS.agenda.util.Constants;

public class EventListActivity extends Activity {
	public static List<Event> events;
	private EventAdapter eventAdapter;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_list_layout);
        
        ListView eventListView = (ListView) findViewById(R.id.eventListView);
        EventBL.initialize(this);
        events = EventBL.getEvents();
        eventAdapter = new EventAdapter(this, events);
		eventListView.setAdapter(eventAdapter);
		
		ImageView addButton = (ImageView) findViewById(R.id.addEventNameImageButton);
		
		addButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				EditText eventNameEditText = (EditText) findViewById(R.id.eventNameEditText);
				if (eventNameEditText.getText().toString().equals("")){
					Toast.makeText(getApplicationContext(), Constants.EVENT_NAME_EMPTY, Toast.LENGTH_LONG).show();
				}
				else {
					Event newEvent = new Event();
					newEvent.setName(eventNameEditText.getText().toString());
					EventBL.insertEvent(newEvent);
					eventNameEditText.setText("");
					eventAdapter.notifyDataSetChanged();
				}
			}
		});
		
		eventListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> adapter, View arg1,
					int position, long id) {
				eventAdapter.notifyDataSetChanged();
				EventHeaderActivity.event = (Event) adapter.getItemAtPosition(position);
				Intent eventHeaderActivityIntent = new Intent(getApplicationContext(), EventHeaderActivity.class);
				startActivity(eventHeaderActivityIntent);
			}
		});
    }
}